package com.wlyuan.open.scanner.application.listener.runner;

import com.wlyuan.open.scanner.application.factory.ScannerHandlerFactory;
import com.wlyuan.open.scanner.config.ScannerProperties;
import com.wlyuan.open.scanner.constant.ScannerCodeEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @author lijing
 * @description
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class ScannerRunListener implements CommandLineRunner {
    private final ScannerProperties scannerProperties;

    @Override
    public void run(String... args) throws Exception {

        //扫描开关，注意：开启后，将会扫描同步数据，对目标数据存在覆盖的风险，请考虑当前运行环境是否需要开启
        if (scannerProperties.isEnable()) {
            // 扫描数据时，可能不是跑所有业务的数据，open 控制是否需要迁移相对应的业务（取找对应的实现类）
            var list = Arrays.asList(ScannerCodeEnum.values()).stream().filter(scannerCodeEnum -> scannerCodeEnum.isOpen()).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(list)) {
                return;
            }

            list.stream().forEach(scanner -> {
                //开始扫描初始化数据
                logger.info("************************************************************");
                logger.info("开始全量同步新系统 {} 数据", scanner.getName());
                ScannerHandlerFactory.getHandler(scanner.getVal()).scanner();

            });
        }

    }

}
